package com.tgu.home_action.controller;

import com.tgu.home_action.pojo.ResponseData;
import com.tgu.home_action.pojo.Volunteer;
import com.tgu.home_action.pojo.VolunteerInformation;
import com.tgu.home_action.pojo.annotation.ApiJsonObject;
import com.tgu.home_action.pojo.annotation.ApiJsonProperty;
import com.tgu.home_action.pojo.annotation.MyLog;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.VolunteerInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.ConstraintViolationException;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Api(tags = "志愿者端的信息管理")
@Validated
@RestController
@RequestMapping("/volunteer/information")
public class VolunteerInformationController {
    @Autowired
    private Logger logger;
    @Autowired
    private VolunteerInformationService volunteerInformationService;

    @ApiOperation("查询个人信息")
    @GetMapping
    @PreAuthorize("hasRole('volunteer')")
    public ResponseData<Volunteer> getInformation(@ApiIgnore @ModelAttribute("volunteerUser") Volunteer volunteerUser) {
        return volunteerInformationService.getInformation(volunteerUser);
    }


    @MyLog(actionId = 6, role = "volunteer", type = 2)
    @ApiOperation("绑定志愿者身份")
    @PreAuthorize("hasRole('volunteer')")
    @ApiResponses({
            @ApiResponse(message = "该志愿者不存在", code = 125),
            @ApiResponse(message = "该志愿者信息已被绑定", code = 127)
    })
    @PostMapping
    public ResponseData<VolunteerInformation> bindInformation(@ApiJsonObject(name = "BindVolunteerInformation", value = {
            @ApiJsonProperty(key = "name", description = "姓名"),
            @ApiJsonProperty(key = "IDCard", description = "身份证号")
    }) @RequestBody Map<String, Object> param, @ApiIgnore @ModelAttribute("volunteerUser") Volunteer volunteerUser) {
        String name = (String) param.get("name");
        String IDCard = (String) param.get("IDCard");
        return volunteerInformationService.bindInformation(name, IDCard, volunteerUser);
    }

    @MyLog(actionId = 4, role = "volunteer", type = 3)
    @ApiOperation("修改个人信息")
    @PreAuthorize("hasRole('volunteer')")
    @ApiResponses(value = {
            @ApiResponse(message = "未绑定志愿者信息", code = 126)
    })
    @PutMapping
    public ResponseData<Object> updateInformation(@Valid @RequestBody VolunteerInformation volunteerInformation, @ApiIgnore @ModelAttribute("volunteerUser") Volunteer volunteerUser) {
        return volunteerInformationService.updateInformation(volunteerInformation, volunteerUser);
    }

    @ApiOperation("解除绑定时获取验证码")
    @PreAuthorize("hasRole('volunteer')")
    @GetMapping("getUnbindCode")
    public ResponseData<Object> getUnbindCode(@ApiIgnore @ModelAttribute("volunteerUser") Volunteer volunteerUser) {
        return volunteerInformationService.getUnbindCode(volunteerUser);
    }

    @MyLog(actionId = 6, role = "volunteer", type = 4, comment = "解除绑定")
    @ApiOperation("解除绑定")
    @PreAuthorize("hasRole('volunteer')")
    @ApiResponses(value = {
            @ApiResponse(message = "未绑定志愿者信息", code = 126)
    })
    @DeleteMapping
    public ResponseData<Object> unbindInformation(@ApiJsonObject(name = "VolunteerUnbindCode", value =
    @ApiJsonProperty(key = "code", description = "验证码")) @RequestBody Map<String, Object> param, @ApiIgnore @ModelAttribute("volunteerUser") Volunteer volunteerUser) {
        return volunteerInformationService.unbindInformation((String) param.get("code"), volunteerUser);
    }

    @ApiOperation("查询所有志愿者")
    @GetMapping("all")
    public ResponseData<List<VolunteerInformation>> getAllVolunteer(VolunteerInformation volunteerInformation) {
        return volunteerInformationService.getAllVolunteer(volunteerInformation);
    }

    @ApiOperation("通过ID查询志愿者信息")
    @GetMapping("id")
    public ResponseData<VolunteerInformation> getVolunteerById(@RequestParam Integer informationId) {
        return volunteerInformationService.getVolunteerById(informationId);
    }

}
